package com.app.location;

import java.util.ArrayList;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import com.app.campusguide.Class_view;
import com.app.campusguide.R;
import com.app.sqlite_connect.HandlerSqlite;

public class MyLocationListener extends Activity implements LocationListener {
	private Location loc_CurrentLocation = null;
	private final String APP_LOG = "MyLocationListener";
	private Location loc_current_location;
	private HandlerSqlite database;
	private Context ctx;
	private ProgressDialog pd;
	private TextView txtv_distance_to;
	private TextView txtv_classroom_level;
	private final float f_avg_altitude_for_level = 2.45F;
	
	public MyLocationListener(Context ctx, ProgressDialog pd, Location loc_current_location, TextView txtv_distance_to,TextView txtv_classroom_level){
		super();
		this.ctx = ctx;
		this.pd = pd;
		this.loc_current_location = loc_current_location;
		database = new HandlerSqlite(this.ctx);
		this.txtv_distance_to = txtv_distance_to;
		this.txtv_classroom_level = txtv_classroom_level;
	}
	
    @Override
    public void onLocationChanged(Location loc) {
        if (loc != null) {
            loc_CurrentLocation = loc;
            loc_current_location = loc;
            pd.dismiss();
            
            ArrayList<Class_view> classes_to_take = database.queryNextClassView();
            Location loc_classroom = new Location(LocationManager.GPS_PROVIDER);
    		loc_classroom.setLatitude(Double.valueOf(classes_to_take.get(0).getStr_site_latitude()));
            loc_classroom.setLongitude(Double.valueOf(classes_to_take.get(0).getStr_site_longitude()));
            
			if(loc_current_location != null){
    			float f_distance = loc_current_location.distanceTo(loc_classroom);
    			Float f_altitude = (float)classes_to_take.get(0).getSh_level();
    			//If the altitude is defined, the distance calculated using the Pythagorean theorem, otherwise, is calculated linearly
    			if(f_altitude != null){
    				float f_hypotenuse = (float)Math.sqrt(Math.pow(f_distance, 2.0) + Math.pow(f_altitude*f_avg_altitude_for_level, 2.0));
    				txtv_distance_to.setText(R.string.str_approx_distance);
    				txtv_distance_to.setText(txtv_distance_to.getText()+" "+f_hypotenuse);
    				TextView txtv_tmp = new TextView(ctx);
    				txtv_tmp.setText(R.string.str_meters);
    				txtv_distance_to.setText(txtv_distance_to.getText()+" "+txtv_tmp.getText());
    				txtv_classroom_level.setText(R.string.str_at_level);
    				txtv_classroom_level.setText(txtv_classroom_level.getText()+" "+classes_to_take.get(0).getSh_level());
    			}else{
    				txtv_distance_to.setText(R.string.str_approx_distance);
    				txtv_distance_to.setText(txtv_distance_to.getText()+" "+f_distance);
    				TextView txtv_tmp = new TextView(ctx);
    				txtv_tmp.setText(R.string.str_meters);
    				txtv_distance_to.setText(txtv_distance_to.getText()+" "+txtv_tmp.getText());
    			}
            }  		
    		
        }else{
        	Toast.makeText(getBaseContext(), getResources().getString(R.string.gps_signal_not_found),Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub
    }

	@Override
	public void onProviderEnabled(String provider) {
	    // TODO Auto-generated method stub
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub
    }

	public Location getLoc_CurrentLocation() {
		return loc_CurrentLocation;
	}

	public void setLoc_CurrentLocation(Location loc_CurrentLocation) {
		this.loc_CurrentLocation = loc_CurrentLocation;
	}
} 